package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.ReturnListGoodsDao;
import com.atguigu.jxc.dao.UserDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.ReturnListGoodsService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.print.attribute.HashAttributeSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author :ttm
 * @description :
 */
@Service
public class ReturnListGoodsServiceImpl implements ReturnListGoodsService {
    @Autowired
    private ReturnListGoodsDao returnListGoodsDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private LogService logService;

    @Override
    public ServiceVO save(ReturnList returnList, String returnListGoodsStr) {
        Gson gson = new Gson();
        List<ReturnListGoods> returnListGoodsList = gson.fromJson(returnListGoodsStr, new TypeToken<List<ReturnListGoods>>() {
        }.getType());
        User currentUser = userDao.findUserByName((String) SecurityUtils.getSubject().getPrincipal());
        returnList.setUserId(currentUser.getUserId());
        returnListGoodsDao.saveReturnList(returnList);
        for (ReturnListGoods returnListGoods : returnListGoodsList) {
            returnListGoods.setReturnListId(returnList.getReturnListId());
            returnListGoods.setGoodsTypeId(goodsDao.findByGoodsId(returnListGoods.getGoodsId()).getGoodsTypeId());
            returnListGoodsDao.saveReturnListGoods(returnListGoods);
            Goods goods = goodsDao.findByGoodsId(returnListGoods.getGoodsId());
            goods.setInventoryQuantity(goods.getInventoryQuantity() - returnListGoods.getGoodsNum());
            goods.setState(2);
            goodsDao.updateGoods(goods);
        }
        logService.save(new Log(Log.INSERT_ACTION, "新增商品退货单：" + returnList.getSupplierName()));
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    //退货单列表展示（可条件查询：单据号模糊、供应商、是否付款和日期区间）
    public Map<String, Object> list(String returnNumber, Integer supplierId, Integer state, String sTime, String eTime) {
        HashMap<String, Object> map = new HashMap<>();
        List<ReturnList> returnListList = returnListGoodsDao.getReturnlist(returnNumber, supplierId, state, sTime, eTime);
        map.put("rows", returnListList);
        logService.save(new Log(Log.SELECT_ACTION, "退货单列表信息"));
        return map;
    }

    @Override
    public Map<String, Object> goodsList(Integer returnListId) {
        HashMap<String, Object> map = new HashMap<>();
        List<ReturnListGoods> returnListGoodsList=returnListGoodsDao.getReturnListGoodsByReturnListId(returnListId);
        map.put("rows",returnListGoodsList);
        logService.save(new Log(Log.SELECT_ACTION, "退货单商品列表信息"));
        return map;
    }

    @Override
    public ServiceVO delete(Integer returnListId) {
        returnListGoodsDao.deleteReturnListGoodsByReturnListId(returnListId);
        returnListGoodsDao.deleteReturnListById(returnListId);
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }
}
